package org.zerta.troops.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.zerta.troops.common.Strings;
import org.zerta.troops.network.TroopsNetworkClient;

public class LobbyPanel extends JPanel implements ActionListener {

	static final long serialVersionUID = 1L;
	
	JTextField playerName;
	JTextField serverHostname;
	JTextField portNumber;
	TroopsNetworkClient troopsNetworkClient;
	
	LobbyPanel (TroopsNetworkClient troopsNetworkClient) {

		this.troopsNetworkClient = troopsNetworkClient;
		JLabel label = new JLabel(Strings.Network.LOBBYPANEL_NAME_TEXT);
		add(label);
	
		JLabel playerNameLabel = new JLabel(Strings.PLAYER_NAME_TEXT);
		add(playerNameLabel);
		playerName = new JTextField(10);
		add(playerName);
		
		serverHostname = new JTextField(Strings.Network.DEFAULT_HOSTNAME_TEXT);
		add(serverHostname);
		
		portNumber = new JTextField(Strings.Network.DEFAULT_PORT_NUMBER_TEXT);
		add(portNumber);
		
		JButton commitButton = new JButton(Strings.Network.CONNECT_TO_GAME_TEXT);
		commitButton.setActionCommand(Strings.Network.CONNECT_TO_GAME);
		add(commitButton);
		commitButton.addActionListener(this);
	}
	
	public String getWorldName() {
		// FIXME return real world name
		return "Colors";
	}
	
	public String getPlayerName() {
		// FIXME return real name
		return new String();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(Strings.Network.CONNECT_TO_GAME)) {
			
			troopsNetworkClient.setHostname(serverHostname.getText());
			troopsNetworkClient.setPort(new Integer(portNumber.getText()).intValue());
			troopsNetworkClient.joinGame(playerName.getText());
		}

	}
	
	String getMapFilename() {
		// FIXME return real map filename
		return "resources/worlds/"+getWorldName()+".xml";
	}

}
